#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
bool chk_ind(vector<vector<int>> dict, vector<vector<int>> s_trans){
for(int i=0; i<s_trans.size(); ++i){
sort(s_trans[i].begin(), s_trans[i].end());
sort(dict[i].begin(), dict[i].end());
}
for(int i=0; i<dict.size(); ++i){
if(find(s_trans.begin(), s_trans.end(), dict[i])==s_trans.end()) return false;
}
return true;
}
vector<int> solution(string s){
vector<int> answer, tmp;
vector<vector<int>> s_trans;
for(int i=0; i<s.length();){
if(s[i]=='{') {tmp.clear(); ++i; continue;}
if(s[i]=='}') {
if(s[i+1]!='}'){s_trans.push_back(tmp);}
++i;
continue;
}
if(s[i]==',') {++i; continue;}
if(s[i]>='0' && s[i]<='9'){
string num(1, s[i]);
while(s[++i]>='0' && s[i]<='9') num+=s[i];
if(find(answer.begin(), answer.end(), stoi(num))==answer.end()){
answer.push_back(stoi(num));
}
tmp.push_back(stoi(num));
}
}
sort(answer.begin(), answer.end());
do{
vector<vector<int>> dict;
for(int i=1; i<answer.size()+1; ++i){
vector<int> sub;
for(int j=0; j<i; ++j) sub.push_back(answer[j]);
dict.push_back(sub);
}
if(chk_ind(dict, s_trans)) return answer;
}while(next_permutation(answer.begin(), answer.end()));
answer.clear();
return answer;
}
void print_vec(vector<int> result){
if(!result.size()){
cout<<"NO VEC"<<endl;
return;
}
cout<<"[";
for(int i=0; i<result.size()-1; ++i) cout<<result[i]<<", ";
cout<<result[result.size()-1]<<"]"<<endl;
}
int main(void){
string s1="{{2},{2,1},{2,1,3},{2,1,3,4}}";
string s2="{{1,2,3},{2,1},{1,2,4,3},{2}}";
string s3="{{20,111},{111}}";
string s4="{{123}}";
string s5="{{4,2,3},{3},{2,3,4,1},{2,3}}";
print_vec(solution(s1));
print_vec(solution(s2));
print_vec(solution(s3));
print_vec(solution(s4));
print_vec(solution(s5));
return 0;
}